package com.tcy.game.angryrobots.general;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Align;
import com.tcy.game.angryrobots.Assets;

/**
 * SimpleButton
 * Created by 80002023 on 2016/7/27.
 */
public class SimpleButton {

    private String text;
    private boolean wasPressed;
    private float x;
    private float y;
    private float w;
    private float h;
    private boolean activated;
    private boolean down;
    private BitmapFont font;
    private float textHeight;

    public SimpleButton(String text, BitmapFont font) {
        this.text = text;
        this.wasPressed = false;
        this.activated = false;
        this.down = false;
        this.font = font;
        GlyphLayout glyphLayout = new GlyphLayout();
        glyphLayout.setText(Assets.textFont, text);

        textHeight = glyphLayout.height;
        w = glyphLayout.width * 2;
        h = glyphLayout.height * 2;

        CommonUtils.log("SimpleButton", "construct width: " + w + ", height: " + h);
    }

    public void setWidth(float width) {
        w = width;
    }

    public void setHeight(float height) {
        h = height;
    }


    public void update(float delta, boolean justTouched, boolean isTouched, boolean justReleased, float x, float y) {
        wasPressed = false;
        if (justTouched && inBounds(x, y)) {
            activated = true;
            down = true;
        } else if (isTouched) {
            down = activated && inBounds(x, y);
        } else if (justReleased) {
            wasPressed = activated && inBounds(x, y);
            activated = false;
            down = false;
        } else {
            activated = false;
        }
    }

    private boolean inBounds(float x, float y) {
        return x >= this.x && x < this.x + this.w && y >= this.y && y < this.y + this.h;
    }

    public void draw(SpriteBatch spriteBatch) {
        Color oldColor = font.getColor();
        if (down) {
            spriteBatch.setColor(Color.RED);
        } else {
            spriteBatch.setColor(Color.BLUE);
        }
        spriteBatch.draw(Assets.pureWhiteTextureRegion, x, y, w, h);
//        spriteBatch.draw(Assets.pureWhiteTextureRegion, x, y + 20, w, h - 8);
        spriteBatch.setColor(Color.WHITE);
        if (down) {
            font.setColor(oldColor.r / 2, oldColor.g / 2, oldColor.b / 2, oldColor.a);
        }
        float textX = x;
        float textY = y + h;
        textY -= (h - textHeight) / 2;
        //CommonUtils.log("SimpleButton","text x: " + textX + ", y: " + textY);
        font.getData().setScale(.2f);
        font.draw(spriteBatch, text, textX, textY, w, Align.center, false);

        font.setColor(oldColor);

    }

    public boolean wasPressed() {
        return this.wasPressed;
    }

    public void rightOn(float right) {
        x = right - w;
    }

    public void leftOn(float left) {
        x = left;
    }

    public void centerHorizontallyOn(float centerX) {
        x = centerX - w / 2;
    }

    public void bottomOn(float bottom) {
        y = bottom;
    }

    public void topOn(float top) {
        y = top - h;
    }

    public void centerVerticallyOn(float centerY) {
        y = centerY - h / 2;
    }
}
